package com.chuangshixinchen.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangshixinchen.enterprise.common.*;
import com.chuangshixinchen.enterprise.entity.Tb_user;
import com.chuangshixinchen.enterprise.mapper.Tb_userMapper;
import com.chuangshixinchen.enterprise.service.ITb_userService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangshixinchen.enterprise.vo.req.UserChangePwdVo;
import com.chuangshixinchen.enterprise.vo.req.UserLoginVo;
import com.chuangshixinchen.enterprise.vo.req.UserReqPageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-08-13
 */
@Service
public class Tb_userServiceImpl extends ServiceImpl<Tb_userMapper, Tb_user> implements ITb_userService {

    @Override
    public Result<Page<Tb_user>> listPage(UserReqPageVo vo) {
        Page<Tb_user> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        LambdaQueryWrapper<Tb_user> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(vo.getSearchKey())){
            wrapper.like(Tb_user::getNickName, vo.getSearchKey());
        }
        Page<Tb_user> userPage = baseMapper.selectPage(page, wrapper);
        return new Result<>(true, StatusCode.OK,"查询成功",userPage);
    }

    @Override
    public Result<Tb_user> login(UserLoginVo vo) {
        String saltPwd = MD5.generateSalt(vo.getPassword());
        LambdaQueryWrapper<Tb_user> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(vo.getNickName())){
            wrapper.eq(Tb_user::getNickName, vo.getNickName());
        }
        if (StringUtils.hasLength(vo.getPhone())){
            wrapper.eq(Tb_user::getPhone, vo.getPhone());
        }
        wrapper.eq(Tb_user::getPassword,saltPwd);
        Tb_user user = baseMapper.selectOne(wrapper);
        if (user==null){
            return new Result<>(false,StatusCode.LOGIN_ERROR,"用户名或密码不正确");
        }
        user.setToken(JwtUtils.getJwtToken(user));
        return new Result<>(true,StatusCode.OK,"登录成功",user);
    }

    @Override
    public Result changePwd(String token,UserChangePwdVo vo) {
        String saltOriginalPwd = MD5.generateSalt(vo.getOldPwd());
        //查询原用户名密码是否正确
        LambdaQueryWrapper<Tb_user> wrapper = new LambdaQueryWrapper();
        Integer userId = JwtUtils.getUserIdByJwtToken(token);
        wrapper.eq(Tb_user::getId,userId);
        wrapper.eq(Tb_user::getPassword,saltOriginalPwd);
        Tb_user user = baseMapper.selectOne(wrapper);
        if (user==null){
            return new Result<>(false,StatusCode.ERROR,"用户名或密码不正确");
        }
        //修改密码
        String saltNewPwd = MD5.generateSalt(vo.getNewPwd());
        user.setPassword(saltNewPwd);
        int changeNum = baseMapper.updateById(user);
        if (changeNum==0){
            return new Result<>(false,StatusCode.ERROR,"密码修改失败");
        }
        return new Result<>(true,StatusCode.OK,"密码修改成功");
    }

    @Override
    public Result<Tb_user> getInfo(Long id) {
        Tb_user user = baseMapper.selectById(id);
        if (user==null){
            return new Result<Tb_user>(false,StatusCode.ERROR,"无此用户");
        }
        return new Result<Tb_user>(true,StatusCode.OK,"查询成功",user);
    }

    @Override
    public Result saveInfo(Tb_user vo) {
        //新增用户时，密码设置为123456
        String saltPwd = MD5.generateSalt("123456");
        vo.setPassword(saltPwd);
        int changeNum = baseMapper.insert(vo);
        if (changeNum==0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @Override
    public Result updateInfo(Tb_user vo) {
        int changeNum = baseMapper.updateById(vo);
        if (changeNum==0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }
}
